// 管理购物车的reducer
// 默认状态
let defaultState = {
  cartList: ["辣条", "牛奶", "可乐"],
};

// 购物车reducer  是一个函数
let cartReducer = (state = defaultState, action) => {
  switch (action.type) {
    case "ADD_CART":
      // 先拷贝（多层 用json）
      let newState = JSON.parse(JSON.stringify(state));
      // 加购物车
      newState.cartList.push(action.name);
      return newState;
    case "REMOVE_CART":
      let cartState = JSON.parse(JSON.stringify(state));
      cartState.cartList.splice(action.index, 1);
      return cartState;
    default:
      return state;
  }
  return state;
};

export default cartReducer;
